<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@taglib prefix="widget" uri="/widget" %>

<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/<spring:theme code="stylesheet.wraper" />" />
<link rel="stylesheet" type="text/css" href="<c:url value="/static/assets/jQueryFileUpload/css/jquery.fileupload.css" />" />

<div class="wraper">
<div class="inner-menu">
    <div class="header">
        <span><spring:message code="docrepository.file.title.allFiles" /></span>
        <a href="${pageContext.request.contextPath}/user/files/edit" class="pull-right" title="<spring:message code="docrepository.file.action.addFile"/>"><i class="glyphicon glyphicon-plus"></i></a>
    </div>
    <a href="<c:url value="/user/files" />" class="item active"><spring:message code="docrepository.file.title.userFiles" /></a>
    <a href="<c:url value="/user/files/favorite" />" class="item"><spring:message code="docrepository.file.title.favoriteFiles" /></a>
</div>

<div class="content">
    <c:if test="${not empty errorNo}">
        <div class="alert alert-danger tc"><spring:message code="docrepository.errorNo.${errorNo}" text="" /></div>
    </c:if>
    <form action='<widget:actionUrl mode="/user/files/doUserFileEdit" />' method="post" class="form">
        <div class="form-group input-group">
            <div class="input-group-btn">
                <input type="hidden" name="directory" value="" />
                <button type="button" class="btn btn-default dropdown-toggle button" data-toggle="dropdown">
                    <span id="typeSelection"><spring:message code="docrepository.file.type.file" /></span>
                    <span class="caret"></span>
                </button>
                <ul class="dropdown-menu" role="menu">
                    <li><a href="javascript:file.changeTypeSelection('0');"><spring:message code="docrepository.file.type.file" /></a></li>
                    <li><a href="javascript:file.changeTypeSelection('1');"><spring:message code="docrepository.file.type.directory" /></a></li>
                </ul>
            </div>
            <input id="fileName" name="fileName" readonly class="form-control input" placeholder="<spring:message code="docrepository.file.fileName" />" />
            <input id="filePath" name="filePath" type="hidden" value="" />
            <input id="fileSize" name="fileSize" type="hidden" value="0" />
            <input id="fileType" name="fileType" type="hidden" value="" />
            <span class="input-group-btn fileinput-button">
                <span class="btn btn-primary button">
                    <i class="glyphicon glyphicon-folder-open"></i>
                    <span class="pl5"><spring:message code="docrepository.common.action.browse" /></span>
                    <input type="file" id="fileupload" multiple/>
                </span>
            </span>
        </div>
        <div class="form-group">
            <label for="descriptiop" class="sr-only"><spring:message code="docrepository.file.description" /></label>
            <textarea id="descriptiop" name="descriptiop" class="form-control input" rows="5" placeholder="<spring:message code="docrepository.file.description" />"></textarea>
        </div>
        <div class="center-block">
            <button type="submit" class="btn btn-default"><spring:message code="docrepository.common.action.submit" /></button>
            <button type="button" class="btn btn-default"><spring:message code="docrepository.common.action.cancel" /></button>
        </div>
    </form>
</div>
</div>
<script type="text/javascript" src="<c:url value="/static/assets/jQueryFileUpload/js/vendor/jquery.ui.widget.js" />"></script>
<script type="text/javascript" src="<c:url value="/static/assets/jQueryFileUpload/js/jquery.iframe-transport.js" />"></script>
<script type="text/javascript" src="<c:url value="/static/assets/jQueryFileUpload/js/jquery.fileupload.js" />"></script>

<script style="text/javascript">
var file = file || {
	type: {'0':'<spring:message code="docrepository.file.type.file" />', '1':'<spring:message code="docrepository.file.type.directory" />'}
};
// 更改选择的上传类型:文件、文件夹
file.changeTypeSelection = function(type){
	if(type && file.type && file.type[type]){
		$('#typeSelection').html(file.type[type]);
		$('input[name=directory]').val((type == '1'));
		if('1' == type){
		    // 文件夹时，需要隐藏上传按钮, 并且fileName字段可写
		    $('.fileinput-button').hide();
		    $('input[name=fileName]').attr('readonly', false);
		} else{
			$('.fileinput-button').show();
			$('input[name=fileName]').attr('readonly', true);
		}
	}
};

$(function () {
    // 文件上传
	$('#fileupload').fileupload({
    	url: '${pageContext.request.contextPath}/fileUpload?uploaderConfigKey=userFile',
        done: function (e, data) {
        	if(data && data.result) {
        		try {
        			var result = $.parseJSON(data.result);
        			if(result.resultCode && result.resultCode == 'FU00001'){
            			// 成功处理
            			if(result.data){
            				$.each(result.data, function (index, file) {
            					$('#fileName').val(file.fileName || '');
            					$('#filePath').val(file.filePath || '');
            					$('#fileSize').val(file.size || '');
            					$('#fileType').val(file.type || '');
                            });
            			}
            		} else if(result.message){
            			alert(result.message);
            		}
        		} catch(er){
        		}
        	}
        }
    });
});
</script>